package org.ascollada.fx
{
	import org.ascollada.core.DaeDocument;
	import org.ascollada.core.DaeElement;
	import org.ascollada.core.ns.collada;
	import org.ascollada.utils.DaeLogger;
	
	public class DaeBindMaterial extends DaeElement
	{
		use namespace collada;
		
		public var instance_material :Array;
		
		/**
		 * 
		 */ 
		public function DaeBindMaterial(document:DaeDocument, element:XML=null)
		{
			super(document, element);
		}
		
		/**
		 * 
		 */ 
		public function findMaterialInstance(symbol:String):DaeInstanceMaterial
		{
			for each(var m :DaeInstanceMaterial in this.instance_material)
			{
				if(m.symbol == symbol)
					return m;
			}
			return null;
		}
		
		/**
		 * 
		 */ 
		public override function read(element:XML):void
		{
			super.read(element);
			
			this.instance_material = new Array();
			
			if(element.technique_common[0])
			{
				readTechniqueCommon(element.technique_common[0]);	
			}
			else
			{
				DaeLogger.log("[ERROR] DaeBindMaterial expected a <technique_common> element!");
			}
		}
		
		/**
		 * 
		 */
		private function readTechniqueCommon(element:XML):void
		{
			var list :XMLList = element.instance_material;
			var i :int;
			
			for(i = 0; i < list.length(); i++)
			{
				this.instance_material.push(new DaeInstanceMaterial(document, list[i]));
			}
		} 
	}
}